package com.ruoyi.business.ajax;

import com.alibaba.excel.event.Order;
import com.ruoyi.business.ajax.request.BasicRequest;
import com.ruoyi.business.ajax.request.OrderRequest;
import com.ruoyi.business.domain.*;
import com.ruoyi.business.service.*;
import com.ruoyi.common.annotation.AjaxLogin;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AjaxLogin
@RestController
@RequestMapping("/ajax/order")
public class AjaxOrderController extends AuthController {

    @Resource
    private IBizOrderService bizOrderService;

    @Resource
    private IBizProductService bizProductService;

    @Resource
    private IBizMemberService bizMemberService;

    @Resource
    private IBizMemberAddressService bizMemberAddressService;


    //我的订单(status-1取全部订单)
    @PostMapping("/list")
    public AjaxResult list()
    {
        Long userID = getUserID();
        BizOrder param = new BizOrder();
        param.setMemberId(userID);
        List<BizOrder> orderList = bizOrderService.selectBizOrderList(param);
        List<Map> resultList = new ArrayList<>();
        for (BizOrder order : orderList) {
            Map item = new HashMap();
            item.put("id", order.getId());
            item.put("sn", order.getOrderSn());
            item.put("status", order.getOrderStatus());
            item.put("productName", order.getProductName());
            item.put("productImage", order.getProductImage());
            item.put("productNum", order.getProductCount());
            item.put("productPrice", order.getProductAmount());
            item.put("orderPrice", order.getOrderAmount());
            resultList.add(item);
        }
        return AjaxResult.success(resultList);
    }

    //我的订单详情
    @PostMapping("/detail")
    public AjaxResult detail(@RequestBody OrderRequest request)
    {
        Long userID = getUserID();
        BizOrder order = bizOrderService.selectBizOrderById(request.getOrderID());
        if (order == null || !userID.equals(order.getMemberId())) {
            return AjaxResult.error();
        }
        return AjaxResult.success(order);
    }

    //订单结账
    @PostMapping("/orderConclude")
    public AjaxResult orderConclude(@RequestBody OrderRequest request)
    {
        Long userID = getUserID();
        Long productID = request.getProductID();
        Integer productNum = request.getProductNum();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //取出福豆余额
        resultMap.put("douBalance", bizMemberService.selectBizMemberDou(userID, BizAccount.DOU_BALANCE));
        //取出默认地址
        BizMemberAddress defaultAddress = bizMemberAddressService.selectDefaultAddressByMemberId(userID);
        resultMap.put("defaultAddress", defaultAddress);
        //取出商品
        BizProduct product = bizProductService.selectBizProductById(productID);
        if (product == null || product.getOnlineStatus() == 0) {    //检测上架
            return AjaxResult.warn("该商品不存在");
        }
        resultMap.put("productName", product.getProductName());
        resultMap.put("productRemark", product.getRemark());
        resultMap.put("productImage", product.getMainImage());
        resultMap.put("productNum", productNum);
        resultMap.put("productPrice", product.getAmount());
        resultMap.put("orderPrice", product.getAmount().multiply(new BigDecimal(productNum)));
        return AjaxResult.success(resultMap);
    }

    //下订单
    @PostMapping("/orderAdd")
    public AjaxResult orderAdd(@RequestBody OrderRequest request)
    {
        Long userID = getUserID();
        if (true) {
            return AjaxResult.warn("系统维护中,无法进行下单操作");
        }
        return bizOrderService.orderAdd(userID, request.getProductID(), request.getProductNum(), request.getAddressID(), request.getRemark());
    }

    //取消订单
    @PostMapping("/orderCancel")
    public AjaxResult orderCancel(Long orderID)
    {
        Long userID = getUserID();

        //TODO 取消订单延后(专项划拨处理??)
        return bizOrderService.orderConfirm(userID, orderID);
    }

    //订单收货
    @PostMapping("/orderConfirm")
    public AjaxResult orderConfirm(@RequestBody OrderRequest request)
    {
        Long userID = getUserID();

        return bizOrderService.orderConfirm(userID, request.getOrderID());
    }
}
